package com.catmiao.spark.framework.common

import com.catmiao.spark.framework.controller.WordCountController
import com.catmiao.spark.framework.util.EnvUtil
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @title: TApplication
 * @projectName spark_study
 * @description: TODO
 * @author ChengMiao
 * @date 2024/3/15 16:12
 */
trait TApplication {


  /**
   * @param master 环境
   * @param appName 应用名称
   * @param op 传递操作
   */
  def start(master:String ="local[*]",appName:String = "Application")(op: => Unit): Unit = {

    val sparkConf = new SparkConf().setMaster(master).setAppName(appName)

    val sparkContext = new SparkContext(sparkConf)

    // 放入ThreadLocal
    EnvUtil.put(sparkContext)

    try {
      op
    } catch {
      case ex => println(ex.getMessage)
    }


    sparkContext.stop();

    // 清除
    EnvUtil.clear

  }

}
